package com.lry.jvm.instructions.constants;


import com.lry.jvm.instructions.base.Index8Instruction;
import com.lry.jvm.rtda.Frame;
import com.lry.jvm.rtda.JObject;
import com.lry.jvm.rtda.OperandStack;
import com.lry.jvm.rtda.heap.ClassRef;
import com.lry.jvm.rtda.heap.JClass;
import com.lry.jvm.rtda.heap.RuntimeConstantPool;
import com.lry.jvm.rtda.heap.StringPool;
import com.lry.jvm.util.Util;

public class ldc extends Index8Instruction {

    @Override
    public byte getOpCode() {
        return 0x12;
    }

    @Override
    public String getReName() {
        return "ldc";
    }

    @Override
    public void execute(Frame frame) {
        OperandStack stack = frame.getOperandStack();
        JClass jClass = frame.getMethod().getJClass();
        RuntimeConstantPool rcp = jClass.getConstantPool();

        Object obj = rcp.getConstant(index);

        if(obj instanceof Integer){
            stack.pushInt((Integer) obj);
        }else if(obj instanceof Float){
            stack.pushFloat((Float) obj);
        }else if(obj instanceof String){
            JObject internedStr = StringPool.JString(jClass.getLoader(), (String) obj);
            stack.pushRef(internedStr);
        }else if(obj instanceof ClassRef){
            ClassRef classRef = (ClassRef) obj;
            JObject classObj = classRef.resolvedClass().getJClass();
            stack.pushRef(classObj);
        }else{
            Util.panic("todo ldc "+obj);
        }
    }
}
